﻿using System;
using System.Text;
using System.Web;
using System.Web.UI;
using Besture.Utils;

namespace Besture.UI.Admin.Control
{
    public partial class UploadMultiPhoto1 : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "text/plain";
            Response.Charset = "utf-8";
            var httpPostedFile = Request.Files["ProductPhotoFile"];
            string yearMonthPath = GetYearMonthPath();
            //上传图片路径
            string normalFolder = Request["FolderPath"] + yearMonthPath;
            //检测上传图片路径
            string absoluteNormalFolderPath = DetectionDirectory(normalFolder);
            if (httpPostedFile == null)
            {
                /*失败发送0*/
                Response.Write("0");
                Response.End();
            }
            //图片名称
            string timestamp = string.Empty;
            string photoFileName = GetPhotoFileName(httpPostedFile, ref timestamp);
            //图片路径（用于发至前台）
            string photoFilePath = yearMonthPath + "/" + photoFileName;
            //正常图片路径 
            string normalPhotoFilePath = absoluteNormalFolderPath + photoFileName;
            httpPostedFile.SaveAs(normalPhotoFilePath);

            //是否需要微信缩略图片
            bool NeedWechatThumbnail = Request["NeedWechatThumbnail"].ToLower().Equals("true");
            if (NeedWechatThumbnail)
            { //生成微信图片
                //检测缩略图上传图片路径 
                string thumbnailWechatFolderPath = Request["ThumbnailWechatFolderPath"];
                //缩略图宽度
                double thumbnailWechatWidth = Convert.ToInt32(Request["ThumbnailWechatWidth"]);
                //缩略图高度
                double thumbnailWechatHeight = Convert.ToInt32(Request["ThumbnailWechatHeight"]);
                //检测上传缩略图片路径
                string absoluteWechatThumbnailFolderPath = DetectionDirectory(thumbnailWechatFolderPath + GetYearMonthPath());
                //缩略图图片路径
                string thumbnailWechatPhotoFilePath = absoluteWechatThumbnailFolderPath + photoFileName;
                DetectionDirectory(thumbnailWechatFolderPath + "/" + GetYearMonthPath());
                //生成缩略图片
                ImageUtil.CreatePic(normalPhotoFilePath, thumbnailWechatPhotoFilePath, thumbnailWechatWidth, thumbnailWechatHeight);

            
            }
            //是否需要缩略图片
            bool NeedThumbnail = Request["NeedThumbnail"].ToLower().Equals("true");
            if (NeedThumbnail)
            {
                //检测缩略图上传图片路径 
                string thumbnailFolderPath = Request["ThumbnailFolderPath"];
                //缩略图宽度
                double thumbnailWidth = Convert.ToInt32(Request["ThumbnailWidth"]);
                //缩略图高度
                double thumbnailHeight = Convert.ToInt32(Request["ThumbnailHeight"]);
                //检测上传缩略图片路径
                string absoluteThumbnailFolderPath = DetectionDirectory(thumbnailFolderPath + GetYearMonthPath());
                //缩略图图片路径
                string thumbnailPhotoFilePath = absoluteThumbnailFolderPath + photoFileName;
                DetectionDirectory(thumbnailFolderPath + "/" + GetYearMonthPath());
                //生成缩略图片
                ImageUtil.CreatePic(normalPhotoFilePath, thumbnailPhotoFilePath, thumbnailWidth, thumbnailHeight);
                if (thumbnailFolderPath.Contains("/Upload/Product/320/"))
                {
                    string absoluteThumbnailFolderPaths = DetectionDirectory(Besture.Common.Definition.Constants.IMAGEFILE_PRODUCT_THUMBNAIL + GetYearMonthPath());
                    //缩略图图片路径
                    string thumbnailPhotoFilePaths = absoluteThumbnailFolderPaths + photoFileName;
                    DetectionDirectory(Besture.Common.Definition.Constants.IMAGEFILE_PRODUCT_THUMBNAIL + "/" + GetYearMonthPath());

                    ImageUtil.CreatePic(normalPhotoFilePath, thumbnailPhotoFilePaths, 320, 231);  
                }
                if (thumbnailFolderPath.Contains("/Upload/Card/320/"))
                {
                    string absoluteThumbnailFolderPaths = DetectionDirectory(Besture.Common.Definition.Constants.IMAGEFILE_CARD_THUMBNAIL + GetYearMonthPath());
                    //缩略图图片路径
                    string thumbnailPhotoFilePaths = absoluteThumbnailFolderPaths + photoFileName;
                    DetectionDirectory(Besture.Common.Definition.Constants.IMAGEFILE_CARD_THUMBNAIL + "/" + GetYearMonthPath());

                    ImageUtil.CreatePic(normalPhotoFilePath, thumbnailPhotoFilePaths, 320, 231);
                }
                if (thumbnailFolderPath.Contains("/Upload/Gift/320/"))
                {
                    string absoluteThumbnailFolderPaths = DetectionDirectory(Besture.Common.Definition.Constants.IMAGEFILE_GIFT_THUMBNAIL + GetYearMonthPath());
                    //缩略图图片路径
                    string thumbnailPhotoFilePaths = absoluteThumbnailFolderPaths + photoFileName;
                    DetectionDirectory(Besture.Common.Definition.Constants.IMAGEFILE_GIFT_THUMBNAIL + "/" + GetYearMonthPath());

                    ImageUtil.CreatePic(normalPhotoFilePath, thumbnailPhotoFilePaths, 320, 231);
                }
                //生成原生缩略图
               
                //图片地址|图片说明|图片编号
                Response.Write(string.Format(@"{0}|{1}|{2}", photoFilePath, "", timestamp));
                Response.End();

            }

           

            //图片地址|图片说明|
            Response.Write(string.Format(@"{0}|{1}|{2}", photoFilePath, "", timestamp));
            Response.End();

            //图片地址|图片说明|
            Response.Write(string.Format(@"{0}|{1}|{2}", photoFilePath, "", timestamp));
            Response.End();


        }
        /// <summary>
        /// 获取文件夹路径
        /// </summary>
        /// <returns></returns>
        public string GetFolderPath(string _folderPath)
        {
            return _folderPath + DateTime.Now.ToString("yyyy-MM-dd") + "/";
        }

        /// <summary>
        /// 获取年月文件夹
        /// </summary>

        /// <returns></returns>
        public string GetYearMonthPath()
        {
            return DateTime.Now.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 检测目录，如果无则创建
        /// </summary>
        public string DetectionDirectory(string path)
        {
            string absolutePath = Server.MapPath(path);
            if (!System.IO.Directory.Exists(absolutePath))
            {
                System.IO.Directory.CreateDirectory(absolutePath);
            }
            return absolutePath + "\\";
        }

        /// <summary>
        /// 获取图片名称
        /// </summary>
        /// <param name="httpPostedFile"></param>
        /// <returns></returns>
        public string GetPhotoFileName(HttpPostedFile httpPostedFile, ref string timestamp)
        {
            timestamp = Fun.GetTimestamp();
            var strEx = (Fun.GetExName(httpPostedFile.FileName)).ToLower();
            return timestamp + strEx;
        }

        /// <summary>
        /// 初始化返回DIV
        /// </summary>
        /// <param name="thumbnailFolderPath"></param>
        /// <param name="photoFilePath"></param>
        /// <returns></returns>
        public string InitDiv(string thumbnailFolderPath, string photoFilePath)
        {
            string photoUrl = thumbnailFolderPath + photoFilePath;
            StringBuilder text = new StringBuilder();
            text.Append("<div class='element'>");
            text.Append("    <div>");
            text.Append("        <img src='" + photoUrl + "' alt='' />");
            text.Append("    </div>");
            text.Append("    <p>");
            text.Append("        <input type='text' id='PPID_Explain_1' name='PPID_Explain_1' value='' />");
            text.Append("    </p>");
            text.Append("    <p class='buttonP'>");
            text.Append("        <img src='/Admin/Images/del.gif' alt='删除商品图片' class='delPhoto' /><a href='javascript:DelProductPhoto('1')'>删除</a>");
            text.Append("    </p>");
            text.Append("</div>");
            text.Append("");
            return text.ToString();
        }
    }
}